import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import edu.um.analyzer.exception.InvalidArrayIndexException;
import edu.um.analyzer.exception.InvalidVariableReferenceException;
import edu.um.analyzer.exception.TypeMismatchException;
import edu.um.analyzer.exception.VariableNotDeclaredException;
import edu.um.expression.Expression;
import edu.um.expression.ExpressionTreeNode;
import edu.um.expression.ExpressionTreeNodeFactory;
import edu.um.token.Tokenizer;
import edu.um.token.exception.InvalidTokenException;
import edu.um.variable.ArrayVariable;
import edu.um.variable.Range;
import edu.um.variable.RecordVariable;
import edu.um.variable.Variable;
import edu.um.variable.VariableType;

public class Test {

  /**
   * @param args
   * @throws IOException
   * @throws InvalidTokenException
   * @throws InvalidVariableReferenceException
   * @throws VariableNotDeclaredException
   * @throws InvalidArrayIndexException
   * @throws TypeMismatchException
   */
  public static void main(String[] args) throws IOException, InvalidTokenException, InvalidVariableReferenceException, InvalidArrayIndexException, VariableNotDeclaredException, TypeMismatchException {
    Map<String, Variable> variableMap = new HashMap<String, Variable>();
    variableMap.put("persona", new Variable("persona",
        VariableType.WORD));
    List<Range> ranges = new ArrayList<Range>();
    ranges.add(new Range(1, 10));
    ranges.add(new Range (151, 190));
    variableMap.put("mes", new ArrayVariable("persona",
        VariableType.STRING_SUBRAYADO, ranges));
    List<Variable> fields = new ArrayList<Variable>();
    fields.add(new Variable ("nombre", VariableType.STRING_SUBRAYADO));
    variableMap.put("usuario", new RecordVariable("usuario", fields));
    ExpressionTreeNodeFactory.setVariablemap(variableMap);
    String textExpression = "2 * persona ^ mes [ 2 , persona ] * ! ! usuario . nombre * ( 30 - 6555 * 32 - 1 )";
    InputStream is = new ByteArrayInputStream(textExpression.getBytes("UTF-8"));
    Tokenizer tokenizer = new Tokenizer(is);
    Expression expression = new Expression();
    while (tokenizer.hasMoreToken()) {
      expression.add(tokenizer.nextToken());
    }
    ExpressionTreeNode expressionNode = ExpressionTreeNodeFactory
        .createExpressionTreeNode(expression);
    expressionNode.validate();
    System.out.println(expressionNode);
  }

}
